import re

from flask_script import Manager
from flask import Flask, render_template, request
from api import weixin, news_detail, search
from urllib import parse

app = Flask(__name__)

manage = Manager(app)

@app.route("/")
def index():

    if request.url.endswith('/'):
        article = weixin('pc_0', 'pc_0')
    else:
        request_url = request.url
        cid = re.findall('cid=(.*)', request_url)[0]
        article = weixin(cid, cid)
    data = {
        "articles": article
    }
    return render_template('single.html', data=data)


@app.route('/detail')
def detail():
    request_url = request.url
    url = parse.unquote(re.findall("data=(.*)", request_url)[0])
    url = "http://mp.weixin.qq.com/s?" + url
    html = news_detail(url)
    return render_template('detail.html', detail=html)


if __name__ == '__main__':
    manage.run()